package com.zh.work.controller.platform.virtual;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.common.entity.JSONResult;
import com.zh.work.dao.platform.virtual.VirtualWdTransMapper;
import com.zh.work.entity.virtual.VirtualRechargeTrans;
import com.zh.work.entity.virtual.VirtualRechargeTransH;
import com.zh.work.service.platform.virtual.VirtualWdTransService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 账户提现管理控制器
 * @Author: Li.HuiNeng
 * @Create in: 2020-02-13 13:53
 * @Version: 1.0.1
 */
@Api(value = "账户提现管理控制器", produces = "application/json", tags = {"账户提现管理控制器"})
@RestController
@RequestMapping("/wdTrans")
public class VirtualWdTransController {


    @Autowired
    private VirtualWdTransService service;


    @Autowired
    private VirtualWdTransMapper mapper;


    @ApiOperation(value = "分页查询参数数据集", tags = {"getPagesVirtualWdTrans"}, notes = "分页查询参数数据集")
    @GetMapping("/getPagesVirtualWdTrans")
    public JSONResult<List<VirtualRechargeTrans>> getPagesVirtualWdTrans(
            @ApiParam(value = "起始页", name = "pageNo") Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageSize") Integer pageSize,
            @ApiParam(value = "账户信息实体类", name = "VirtualRechargeTrans") VirtualRechargeTrans virtualRechargeTrans) {
        JSONResult<List<VirtualRechargeTrans>> jsonResult = new JSONResult<>();
        try {
            IPage<VirtualRechargeTrans> result = this.service.selectVirtualWdTransPages(new Page<>(pageNo, pageSize), virtualRechargeTrans);
            jsonResult.setTotal(result.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页查询历史参数数据集", tags = {"getPagesVirtualWdTransH"}, notes = "分页查询历史参数数据集")
    @GetMapping("/getPagesVirtualWdTransH")
    public JSONResult<List<VirtualRechargeTransH>> getPagesVirtualWdTransH(
            @ApiParam(value = "起始页", name = "pageNo") Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageSize") Integer pageSize,
            @ApiParam(value = "账户信息历史实体类", name = "VirtualRechargeTrans") VirtualRechargeTransH virtualRechargeTransh) {
        JSONResult<List<VirtualRechargeTransH>> jsonResult = new JSONResult<>();
        try {
            IPage<VirtualRechargeTransH> result = this.service.selectVirtualWdTransHPages(new Page<>(pageNo, pageSize), virtualRechargeTransh);
            jsonResult.setTotal(result.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "查询交易金额统计", tags = {"getRechargeStatistic"}, notes = "查询交易金额统计")
    @GetMapping("/getRechargeStatistic")
    public JSONResult<Object> getRechargeStatistic(@RequestParam Integer type,
                                                   @RequestParam(required = false) String startDateTime,
                                                   @RequestParam(required = false) String endDateTime) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        Map<String, Object> map = new HashMap<>();
        VirtualRechargeTrans virtualRechargeTrans = new VirtualRechargeTrans();
        try {
            if (startDateTime != null) virtualRechargeTrans.setStartDateTime(new SimpleDateFormat().parse(startDateTime));
            if (endDateTime != null) virtualRechargeTrans.setEndDateTime(new SimpleDateFormat().parse(endDateTime));
            if (type == 1) {
                map.put("wdSuccess", this.mapper.getWdTransStatisticService1(virtualRechargeTrans));
                map.put("wdWait", this.mapper.getWdTransStatisticService2(virtualRechargeTrans));
                map.put("wdFail", this.mapper.getWdTransStatisticService3(virtualRechargeTrans));
                map.put("wdError", this.mapper.getWdTransStatisticService4(virtualRechargeTrans));
            }
            if (map != null) {
                jsonResult.setCode(200);
                jsonResult.setMessage("查询完成");
                jsonResult.setSuccess(true);
                jsonResult.setData(map);
            } else {
                jsonResult.setMessage("未查询到相应信息，请检查参数是否正确");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "通过id修改信息", tags = {"putModVirtualRechargeById"}, notes = "通过id修改信息")
    @PutMapping("/putModVirtualRechargeById")
    @ResponseBody
    public JSONResult<Object> putModVirtualRechargeById(@ApiParam(value = "账户信息实体类", name = "virtualRechargeTrans")
                                                        @RequestBody VirtualRechargeTrans virtualRechargeTrans) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        int i = service.putModVirtualRateTypeById(virtualRechargeTrans);
        try {
            if (i > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("修改完成");
                jsonResult.setSuccess(true);
            } else {
                if (i == -1) jsonResult.setMessage("修改费率信息失败!");
                else if (i == -2) jsonResult.setMessage("修改费率类别失败!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "获取交易渠道筛选条件", tags = {"getVirtualTransChannel"}, notes = "获取交易渠道筛选条件")
    @GetMapping("/getVirtualTransChannel")
    public JSONResult<Object> getVirtualTransChannel() {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            List<String> list = this.mapper.getVirtualTransChannelCode();
            List<String> list1 = this.mapper.getVirtualTransChannelMerch();
            Map<String, Object> map = new HashMap<>();
            map.put("channelCode", list);
            map.put("channelMerchNo", list1);
            jsonResult.setData(map);
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

}
